<?php
	require_once 'init.php';
	require_once 'application/user_dao.php';
	
	$message;
	
	if(isset($_REQUEST['user_name']) && isset($_REQUEST['user_password_hidden'])){
		if(!empty($_REQUEST['user_name']) && !empty($_REQUEST['user_password_hidden'])){
			$user_manager = new User_manager($conn);
			try {
				$user = $user_manager->Login($_REQUEST['user_name'],$_REQUEST['user_password_hidden']);
				session_start();
				$_SESSION['user'] = serialize($user);
				header('Location: admin.php');
				exit();
			}
			catch( Exception $e ) {
				$message = $e->getMessage();
			}
		}
	}
?>
<html>
	<head>
		<title>longtailcap.com</title>
		<link rel="stylesheet" type="text/css" href="resources/css/style.css" />
		<script type="text/javascript" src="resources/scripts/sha1.js"></script>
		<script type="text/javascript">
			function encrypt(){
				var pass = document.getElementById("user_password");
				var pass_hidden = document.getElementById("user_password_hidden");
				if(pass.value.length > 0){
					pass_hidden.value = hex_sha1(pass.value);
					pass.value = "";
				}
			}
		</script>
	</head>
	<body>
		<div class="main">
			<div style="text-align: center; padding-top: 20px; margin-bottom: 30px">
				<img src="resources/images/logo.jpg" border="0" />
			</div>
			<form action="admin_login.php" method="post">
				<table align="center" border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td><div style="text-align: right; margin-bottom: 10px; margin-right: 5px">User: </div></td>
						<td><div style="margin-bottom: 10px"><input type="text" name="user_name" /></div></td>
					</tr>
					<tr>
						<td><div style="text-align: right; margin-bottom: 10px; margin-right: 5px">Password: </div></td>
						<td>
							<div style="margin-bottom: 10px">
								<input type="password" name="user_password" id="user_password" />
								<input type="hidden" name="user_password_hidden" id="user_password_hidden" />
							</div>
						</td>
					</tr>
					<tr>
						<td></td>
						<td><input type="submit" value="Login" onclick="encrypt()"/></td>
					</tr>
					<tr>
						<td colspan="2">
							<div style="color: #ff0000; text-align: center; font-size: 12px">
								<? echo(!empty($message)?$message:"");?>
							</div>
						</td>
					</tr>
				</table>
			</form>
		</div>
	</body>
</html>